/*******************************************************************************
filename      TypeDeducer.RegEx.cpp
author        Craig Williams

Brief Description:
  This program attempts to determin what data type the user has entered.

  The following sample driver demonstrates several features of the parser:
    RegEx              - Main driving force that handles the logic to determine
                           what data type was entered.
    Case Insensitivity - Case insensitivity is used to reduce the number of
                           cases that must be handled.
    LoadMemory()       - Loading memory into the parser, rather then a file.
    GetParams()        - Each RegEx is associated with a string. This string
                           is used to determin what data type was deduced.
*******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include "Parser.h"

int main(void)
{
  char  *buffer;
  char   input[1024];
  Parser p(0);

  p.Enable(PARSER_CONST_TOKEN_SEPS);
  p.Enable(PARSER_CONST_LOAD_MEMORY);
  p.Enable(PARSER_CASE_INSENSITIVE);
  p.Enable(PARSER_REGEX);

  p.AddTokenSeparator("\\s+", 0, -1, 0, 0, 0); /* Discard whitespace */
  p.AddTokenSeparator("\\(? *[+-]*[0-9]+\\.?[0-9]*f?[ ,]+([+-]*[0-9]+\\.?[0-9]*f?[ ,]*)+\\)?",
                                                      1, -1, 0, 0, (void *)"Vector");
  p.AddTokenSeparator("[+-]*[0-9]+\\.[0-9]*f\\>",     1, -1, 0, 0, (void *)"float");
  p.AddTokenSeparator("[+-]*[0-9]+\\.[0-9]*",         1, -1, 0, 0, (void *)"double");
  p.AddTokenSeparator("[+-]*[1-9][0-9]*\\>",          1, -1, 0, 0, (void *)"int");
  p.AddTokenSeparator("[+-]*0[0-7]*\\>",              1, -1, 0, 0, (void *)"oct");
  p.AddTokenSeparator("\\<0x[0-9a-fA-F]+\\>",         1, -1, 0, 0, (void *)"hex");
  p.AddTokenSeparator("\\<0b[01]+\\>",                1, -1, 0, 0, (void *)"binary");
  p.AddTokenSeparator("\\<(true|t|false|f)\\>",       1, -1, 0, 0, (void *)"bool");

  printf("                              Data Type Deducer\n\n");
  printf("Type in a number to deduce its data type\n");
  printf("Supported data types: Vector, float, double, int, oct, hex, binary, bool\n");
  printf("Type in \"exit\" to quit\n");
  printf("\n");

  while(1)
  {
    printf("> ");
    input[0] = 0;
    gets(input);
    if(Cmp(input, "exit"))
      break;

    p.LoadMemory(input);
    for(buffer = p.GrabToken(); buffer; buffer = p.GrabToken())
    {
      if(p.GetParams())
        printf("%7s: |%s|\n", (char *)p.GetParams(), buffer);
      else
        printf("%7s: |%s|\n", "Unknown", buffer);
      free(buffer);
    }
    printf("\n");
  }

  return 0;
}
